var Chess = {
    isBlack: false,
    chessArr: [],
    gridCnt : 20,
    gridSize : 25,
    canvas : null,
    initBoard: function() {
        if (this.canvas.getContext) {
            var ctx = this.canvas.getContext("2d");
            this.gridSize = this.canvas.width / this.gridCnt;
            ctx.lineWidth = 1;
            ctx.strokeStyle = "#a0a0a0";
            ctx.beginPath();
            for (var i = 0; i <= this.gridSize; i++) {
                var x = i * this.gridSize;
                ctx.moveTo(x, 0);
                ctx.lineTo(x, this.canvas.height);
                var y = i * this.gridSize;
                ctx.moveTo(0, y);
                ctx.lineTo(this.canvas.width, y);
            }
            ctx.stroke();
        }
    },
    initData: function() {
        for (var i = 0; i < this.gridCnt; i++) {
            this.chessArr[i] = [];
            for (var j = 0; j < this.gridCnt; j++) {
                this.chessArr[i][j] = 0;
            }
        }
    },
    markChess: function(chessX, chessY) {
        if (this.chessArr[chessX][chessY] === 0) {
            var ctx = this.canvas.getContext("2d");
            ctx.beginPath();
            ctx.strokeStyle = "black";
            var circle = {
                x: chessX * this.gridSize, 
                y: chessY * this.gridSize, 
                r: this.gridSize/2 
            };
            ctx.arc(circle.x, circle.y, circle.r, 0, Math.PI * 2, false);
            ctx.stroke();
            if (Chess.isBlack === true) {
                ctx.fillStyle = "black";
                ctx.fill();
                this.chessArr[chessX][chessY] = 1;
                Chess.isWin(chessX, chessY, 1);
                Chess.isBlack = false;
            } else {
                ctx.fillStyle = "white";
                ctx.fill();
                this.chessArr[chessX][chessY] = -1;
                Chess.isWin(chessX, chessY, -1);
                Chess.isBlack = true;
            }
        }
    },
    isWin: function(i, j, chessColor) {
        var nums = 1; 
        var m, n;
        for (m = j - 1; m >= 0; m--) {
            if (this.chessArr[i][m] === chessColor) {
                nums++;
            } else {
                break;
            }
        }
        for (m = j + 1; m < this.gridCnt; m++) {
            if (this.chessArr[i][m] === chessColor) {
                nums++;
            } else {
                break;
            }
        }
        if (nums >= 5) {
            Game.showResult(chessColor);
            return;
        } else {
            nums = 1;
        }
        for (m = i - 1; m >= 0; m--) {
            if (this.chessArr[m][j] === chessColor) {
                nums++;
            } else {
                break;
            }
        }
        for (m = i + 1; m < this.gridCnt; m++) {
            if (this.chessArr[m][j] === chessColor) {
                nums++;
            } else {
                break;
            }
        }
        if (nums >= 5) {
            Game.showResult(chessColor);
            return;
        } else {
            nums = 1;
        }
        for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
            if (this.chessArr[m][n] === chessColor) {
                nums++;
            } else {
                break;
            }
        }
        for (m = i + 1, n = j + 1; m < this.gridCnt && n < this.gridCnt; m++, n++) {
            if (this.chessArr[m][n] === chessColor) {
                nums++;
            } else {
                break;
            }
        }
        if (nums >= 5) {
            Game.showResult(chessColor);
            return;
        } else {
            nums = 1;
        }
        for (m = i - 1, n = j + 1; m >= 0 && n < this.gridCnt; m--, n++) {
            if (this.chessArr[m][n] === chessColor) {
                nums++;
            } else {
                break;
            }
        }
        for (m = i + 1, n = j - 1; m < this.gridCnt && n >= 0; m++, n--) {
            if (this.chessArr[m][n] === chessColor) {
                nums++;
            } else {
                break;
            }
        }
        if (nums >= 5) {
            Game.showResult(chessColor);
            return;
        }
        return false;
    }
};
